package cn.percent.parse;

import cn.percent.config.CacheConfig;
import cn.percent.config.MappedStatement;
import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
import org.apache.ibatis.io.Resources;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.util.Properties;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import javax.xml.namespace.QName;

public class XMLCacheParse {
    private DocumentBuilder documentBuilder;
    private Document document;
    private XPath xpath;
    private static final QName NODE = new QName("http://www.w3.org/1999/XSL/Transform", "NODE");
    public CacheConfig cacheConfig;



    public XMLCacheParse(MappedStatement mappedStatement) throws ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        factory.setValidating(true);
        factory.setNamespaceAware(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(false);
        factory.setExpandEntityReferences(true);
        this.documentBuilder = factory.newDocumentBuilder();
        documentBuilder.setEntityResolver(new XMLMapperEntityResolver());
        XPathFactory xFactory = XPathFactory.newInstance();
        xpath = xFactory.newXPath();
        //this.mappedStatement = mappedStatement;

        //documentBuilder.setEntityResolver();



    }
    public void parse(InputStream inputStream){
        InputSource inputSource = new InputSource(inputStream);
        try{
            document = documentBuilder.parse(inputSource);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void evalNodes(String expression){
        try {
            //return xpath.evaluate(expression,document,NODE);
            Node node = (Node) xpath.evaluate(expression,document,NODE);
            Properties attributes = new Properties();
            NamedNodeMap attributeNodes = node.getAttributes();
            if (attributeNodes != null) {
                for (int i = 0; i < attributeNodes.getLength(); i++){
                    Node attribute = attributeNodes.item(i);
                    String nodeValue = attribute.getNodeValue();
                    attributes.put(attribute.getNodeName(),nodeValue);
                }
            }
            cacheConfig.setBlocking(attributes.getProperty("blocking",null));
            cacheConfig.setEviction(attributes.getProperty("eviction",null));
            cacheConfig.setFlushInterval(attributes.getProperty("flushInterval",null));
            cacheConfig.setReadOnly(attributes.getProperty("readOnly",null));
            cacheConfig.setSize(attributes.getProperty("size",null));
            cacheConfig.setType(attributes.getProperty("type",null));


        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}
